package com.cpecc.dao.wuzi;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.WuZi;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class BaoFeiDao {
	
	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(BaoFei baoFei){
		Session session=sessionFactory.getCurrentSession();
		session.save(baoFei);
		session.flush();
	}
	
	public BaoFei get(String id){
		return (BaoFei) sessionFactory.getCurrentSession().get(BaoFei.class, id);
	}
	
	public boolean cover(BaoFei baoFei){
		String hql="update BaoFei t set t.huanJie=?,t.chuLiFS=?,t.ruKuRQ=?,t.tiChuRQ=?,t.wanChengRQ=?,t.huiShouDW=?,t.huiShouJE=?,t.zongRS=?,t.dangQianS=?,t.caiWuQRR=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, baoFei.getHuanJie());
		query.setString(1, baoFei.getChuLiFS());
		query.setParameter(2, baoFei.getRuKuRQ());
		query.setParameter(3, baoFei.getTiChuRQ());
		query.setParameter(4, baoFei.getWanChengRQ());
		query.setString(5, baoFei.getHuiShouDW());
		query.setDouble(6, baoFei.getHuiShouJE());
		query.setInteger(7, baoFei.getZongRS());
		query.setInteger(8, baoFei.getDangQianS());
		query.setParameter(9, baoFei.getCaiWuQRR());
		query.setString(10, baoFei.getId());
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getMyBaoFei(User user){
		String hql="from BaoFei t where t.user.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();		
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getBaoFeiList(int huanJie){
		String hql="from BaoFei t where t.huanJie.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, huanJie);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getBaoFeiList(int huanJie,User user){
		String hql="from BaoFei t where t.huanJie.id=? and t.caiWuQRR.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, huanJie);
		query.setString(1, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getBaoFeiNeiGouList(int huanJie,User user){
		String hql="from BaoFei t where t.huanJie.id=? and t.user.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, huanJie);
		query.setString(1, user.getId());
		return query.list();
	}

	/**
	 * 报废使用人确认列表
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BaoFei> getBfsyrqrList(User user){
		String hql="from BaoFei t where t.huanJie.id=716 and t.wuZi.user.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getBaoFeiBMList(User user){
		String hql="from BaoFei t where t.huanJie.id=702 and t.user.dep.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getDep().getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getAllBaoFeiKu(WuZi wuZi,String wuZiLBId){
		if(wuZi.getChanPinXLH()==null)
			wuZi.setChanPinXLH("");
		if(wuZi.getGuiGeXH()==null)
			wuZi.setGuiGeXH("");
		if(wuZi.getWuZiMC()==null)
			wuZi.setWuZiMC("");
		if(wuZiLBId==null)
			wuZiLBId="";
		if(wuZiLBId.equals("请选择..."))
			wuZiLBId="";
		String hql="from BaoFei t where t.huanJie.id=704 and t.wuZi.shiYongZT=106 and t.wuZi.chanPinXLH like ? and t.wuZi.guiGeXH like ? and t.wuZi.wuZiMC like ? and t.wuZi.wuZiLB.id like ? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, "%"+wuZi.getChanPinXLH()+"%");
		query.setString(1, "%"+wuZi.getGuiGeXH()+"%");
		query.setString(2, "%"+wuZi.getWuZiMC()+"%");
		query.setString(3, "%"+wuZiLBId+"%");
		return query.list();
		
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getAllByWuZi(WuZi wuZi){
		String hql="from BaoFei t where t.wuZi=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		return query.list();
	}
	
	public boolean hasWuZi(String wuZiId){
		String hql="from BaoFei t where t.wuZi.id=? and t.huanJie.id<713";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiId);
		return query.list().size()>0;
	}
	
	public boolean hasLiuCheng(User user){
		String hql="from BaoFei t where t.user=? and t.huanJie.id!=710";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> activeBaoFei(){
		String hql="from BaoFei t where t.huanJie.id<713";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	public boolean delete(String id) {
		String hql="delete BaoFei t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
}
